<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\common\model\shiyao;

use cores\BaseModel;

/**
 * 模型
 * Class
 * @package app\common\model
 */
class ProjectPay extends BaseModel {
	// 定义表名
	protected $name = 'shiyao_project_pay_log';

	// 定义主键
	protected $pk = 'id';

	/**
	 * 追加字段
	 * @var array
	 */
	protected $append = ['type_text', 'status_text', 'fee_type_text', 'payee_role_text', 'pay_status_pm_text', 'pay_status_text'];
	public function getPayStatusTimeAttr($value) {
		return $value ? Date('Y-m-d', $value) : '';
	}
	public function getCreateTimeAttr($value) {
		return Date('Y-m-d', $value);
	}
	public function getPayAuditTimeAttr($value) {
		return $value ? Date('Y-m-d', $value) : '';
	}
	public function getOutTimeAttr($value) {
		return $value ? Date('Y-m-d', $value) : '';
	}
	public function getPayTimeAttr($value) {
		return $value ? Date('Y-m-d', $value) : '';
	}
	public function getAuditTimePiAttr($value) {
		return $value ? Date('Y-m-d', $value) : '';
	}
	public function getAuditTimePmAttr($value) {
		return $value ? Date('Y-m-d', $value) : '';
	}
	public function getPayStatusTextAttr($value, $data) {
		switch ($data['pay_status']) {
			case 1:
				$type = '待支付';
				break;
			case 2:
				$type = '已支付';
				break;
			case 3:
				$type = '拒绝';
				break;
			default:
				$type = '待支付';
				break;
			}

		return $type;
	}
	public function getPayStatusPmTextAttr($value, $data) {

		switch ($data['pay_status_pm']) {
		case 1:
			$type = '审核通过';
			break;
		case 2:
			$type = '拒绝';
			break;

		default:
			$type = '待审核';
			break;
		}
		return $type;
	}
	public function getStatusTextAttr($value, $data) {
		$type = '未知';
		if ($data['status'] == 10) {
			$type = '待审批';
			if ($data['pi_status'] == 1) {
				$type = '中心通过';
			}
		} elseif ($data['status'] == 11) {
			if ($data['pm_status'] == 1) {
				$type = '申办方通过';
			} else {
				$type = '中心通过';
			}

		} elseif ($data['status'] == 12) {
			if ($data['pi_status'] == 2) {
				$type = '中心拒绝';
			} else {
				$type = '申办方拒绝';
			}

		} elseif ($data['status'] == 20) {
			$type = '支付审批中';
			if ($data['pay_status_pm'] == 1) {
				$type = '待付款';
				if ($data['pay_status_admin'] == 2) {
					$type = '支付申请错误';
				}
			} elseif ($data['pay_status_pm'] == 2) {
				$type = '支付审批拒绝';
			}
		} elseif ($data['status'] == 21) {
			$type = '已支付';
		} elseif ($data['status'] == 30) {
			$type = '已撤销';
		} elseif ($data['status'] == 30) {
			$type = '已撤销';
		}
		return $type;
	}

	public function category() {
		return $this->hasOne('ProjectPayCategory', 'category_id', 'type');
	}

	public function getPayeeRoleTextAttr($value, $data) {
//0普通用户，1CRC，2PI,3PM
		switch ($data['payee_role']) {
		case 1:
			$type = 'CRC';
			break;
		case 2:
			$type = '研究者';
			break;
		case 3:
			$type = 'PM';
			break;

		default:
			$type = '受试者';
			break;
		}
		return $type;
	}

	public function getFeeTypeTextAttr($value, $data) {

		switch ($data['fee_type']) {
		case 1:
			$type = '受试者补助';
			break;

		default:
			$type = '其他';
			break;
		}
		return $type;
	}

	public function center() {
		return $this->hasOne('ProjectCenter', 'id', 'center_id');
	}

	public function project() {
		return $this->hasOne('Project', 'project_id', 'project_id');
	}
	public function pm() {
		return $this->hasOne('Project', 'project_id', 'project_id');
	}

	public function testee() {
		return $this->hasOne('ProjectTestee', 'testee_id', 'testee_id');
	}
	public function payee() {
		$module = self::getCalledModule();
		return $this->hasOne("app\\{$module}\\model\\User", 'user_id', 'payee_id');
	}
	public function refer() {
		$module = self::getCalledModule();
		return $this->hasOne("app\\{$module}\\model\\User", 'user_id', 'refer_id');
	}
	public function auditPi() {
		$module = self::getCalledModule();
		return $this->hasOne("app\\{$module}\\model\\User", 'user_id', 'audit_pi_id');
	}
	public function auditPm() {
		$module = self::getCalledModule();
		return $this->hasOne("app\\{$module}\\model\\User", 'user_id', 'audit_pm_id');
	}
	public function admin() {
		$module = self::getCalledModule();
		return $this->hasOne("app\\{$module}\\model\\store\\User", 'store_user_id', 'admin_id');
	}
	//付款操作
	public function adminPay() {
		$module = self::getCalledModule();
		return $this->hasOne("app\\{$module}\\model\\store\\User", 'store_user_id', 'admin_id_pay');
	}
	/**
	 * 关联隐私图片表
	 * @return \think\model\relation\HasMany
	 */
	public function images() {
		return $this->hasMany('PayImage', 'pay_log_id', 'id')->order(['id']);
	}

	/**
	 * 关联公开凭证图片表
	 * @return \think\model\relation\HasMany
	 */
	public function openImages() {
		return $this->hasMany('PayOpenImage', 'pay_log_id', 'id')->order(['id']);
	}

	public function bank() {
		return $this->hasOne('app\\common\\model\\UploadFile', 'file_id', 'bank_file_id');
	}

	/**
	 * 关联会员记录表
	 * @return BelongsTo
	 */
	public function user() {
		$module = self::getCalledModule();
		return $this->belongsTo("app\\{$module}\\model\\User");
	}

}
